var pool = require("./config/db.js"),
    markdown = require('markdown').markdown;

/**
 * 折扣
 * @param ratings
 * @constructor
 */
function Rates(rates){
    if(rates){
        this.id=rates.id;
        this.hotel_id=rates.hotel_id;
        this.room_type=rates.room_type;
        this.default_price=rates.default_price;
        this.price_monday=rates.price_monday;
        this.price_tuesday=rates.price_tuesday;
        this.price_wednesday=rates.price_wednesday;
        this.price_thursday=rates.price_thursday;
        this.price_friday=rates.price_friday;
        this.price_saturday=rates.price_saturday;
        this.price_sunday=rates.price_sunday;
        this.price_sunday=rates.price_sunday;
    }
};

module.exports = Rates;

/**
 * 增
 * @param callback
 */
Rates.prototype.save = function(callback) {
    var _obj = {
        hotel_id:this.hotel_id,
        room_type:this.room_type,
        default_price:this.default_price,
        price_monday:this.price_monday,
        price_tuesday:this.price_tuesday,
        price_wednesday:this.price_wednesday,
        price_thursday:this.price_thursday,
        price_friday:this.price_friday,
        price_saturday:this.price_saturday,
        price_sunday:this.price_sunday
    };
    pool.getConnection(function(err,connection){
        connection.query('INSERT INTO `ratings` SET ?',
            _obj,
            function (err, rows, fields) {
                if (err) throw err;
                connection.release();
                callback(null,rows);
            });
    });
};

/**
 * 删一个
 * @param callback
 */
Rates.prototype.delete = function(callback) {
    var _id= this.id;
    pool.getConnection(function(err,connection){
        connection.query('DELETE FROM `ratings` WHERE id=?',
            [_id],
            function (err, rows, fields) {
                if (err) throw err;
                connection.release();
                callback(null,rows);
            });
    });
};

/**
 * 改一个
 * @param callback
 */
Rates.prototype.update = function(callback) {
    var _obj = {
        id:this.id,
        hotel_id:this.hotel_id,
        room_type:this.room_type,
        default_price:this.default_price,
        price_monday:this.price_monday,
        price_tuesday:this.price_tuesday,
        price_wednesday:this.price_wednesday,
        price_thursday:this.price_thursday,
        price_friday:this.price_friday,
        price_saturday:this.price_saturday,
        price_sunday:this.price_sunday
    };
    pool.getConnection(function(err,connection){
        connection.query('UPDATE `ratings` SET ? WHERE id =?',
            [
                _obj,
                _obj.id
            ],
            function (err, rows, fields) {
                if (err) throw err;
                connection.release();
                callback(null,rows);
            });
    });
};

/**
 * 查一个
 * @param callback
 */
Rates.prototype.getOne = function(id,callback) {
    var _id= id;
    pool.getConnection(function(err,connection){
        connection.query('SELECT * from `ratings` WHERE id=?',
            [_id]
            , function (err, rows, fields) {
                if (err) throw err;
                connection.release();
                callback(null,rows,_totalPage);
            });
    });
};

/**
 * 分页查
 * @param pagesize 每页个数
 * @param pages    当前页
 * @param callback
 */
Rates.getTen = function(pagesize,pages,callback) {
    var _size = pagesize ? pagesize  : 10,
        _page = pages ? pages : 1,
        _total = null,
        _totalPage=null;

    pool.getConnection(function(err,connection){
        if(err){
            callback("连接池出错");
        }
        if(_total == null){
            connection.query('SELECT COUNT(*) FROM `ratings`',function(err,rows,fields){
                _total = rows[0]['COUNT(*)'];
                _totalPage = _total / _size;
                _query();
            });
        }
        var _query = function(){
            connection.query('SELECT * from `ratings` ORDER BY `id` DESC LIMIT ?,?',
                [_totalPage>=1 ? (_page-1)*_size : 0,
                    _totalPage <_page ? (_page*_size) :_size]
                , function (err, rows, fields) {
                    if (err){
                        callback("查询出错");
                    }
                    connection.release();
                    callback(null,rows);
                });
        }
    });
};
